package com.mxchip.livestarmobile.mobile.ui.login

import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.base.toast
import com.mxchip.livestarmobile.mobile.state.AppConstant
import com.mxchip.livestarmobile.mobile.util.Utils
import com.orhanobut.hawk.Hawk
import io.michaelrocks.libphonenumber.android.PhoneNumberUtil
import kotlinx.android.synthetic.main.fragment_about.*
import kotlinx.android.synthetic.main.phone_number_fragment.*


class PhoneNumberFragment : Fragment() {

    private val viewModel by viewModels<PhoneNumberViewModel>()
    private fun showServiceAndPrivacy() {
        val spannableString = SpannableStringBuilder()

        spannableString.append("我已阅读并同意《服务协议》和《隐私策略》")
        val clickableSpan: ClickableSpan = object : ClickableSpan() {
            override fun onClick(view: View) {
                val action = PhoneNumberFragmentDirections.actionPhoneNumberToWebView()
                action.showWebView = "service_agreement.html"
                action.webViewTitle ="服务协议"
                findNavController().navigate(action)
            }
        }
        val serviceStr = "《服务协议》"
        val serviceIndexOf = spannableString.indexOf(serviceStr);
        spannableString.setSpan(clickableSpan, serviceIndexOf, serviceIndexOf + serviceStr.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)

        val clickableSpan2: ClickableSpan = object : ClickableSpan() {
            override fun onClick(view: View) {
                val action = PhoneNumberFragmentDirections.actionPhoneNumberToWebView()
                action.showWebView = "privacy_policy.html"
                action.webViewTitle ="隐私策略"
                findNavController().navigate(action)
            }
        }
        val privacyStr = "《隐私策略》"
        val privacyIndexOf = spannableString.indexOf(privacyStr)
        spannableString.setSpan(clickableSpan2, privacyIndexOf, privacyIndexOf + privacyStr.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)

        val colorSpan = ForegroundColorSpan(Color.parseColor("#009ad6"))
        spannableString.setSpan(colorSpan, serviceIndexOf, serviceIndexOf + serviceStr.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
        val privacyColorSpan = ForegroundColorSpan(Color.parseColor("#009ad6"))
        spannableString.setSpan(privacyColorSpan, privacyIndexOf, privacyIndexOf + privacyStr.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
        trims_tv.text = spannableString;
        trims_tv.movementMethod = LinkMovementMethod.getInstance();
        (trims_tv as TextView).highlightColor = resources.getColor(android.R.color.transparent)

    }

    private val phoneNumberUtil by lazy {
        PhoneNumberUtil.createInstance(context)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        (activity as AppCompatActivity).supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setDisplayShowHomeEnabled(true)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.phone_number_fragment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        (activity as AppCompatActivity).supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(false)
            it.setDisplayShowHomeEnabled(false)
        }
        showServiceAndPrivacy()
        val agree: Boolean? = Hawk.get(AppConstant.SP_AGREE_TERMS)
        agree_trims_cb.isChecked = !(agree == null || !agree)
        button_get_code.setOnClickListener {
            val checked = agree_trims_cb.isChecked
            if (checked) {
                Hawk.put(AppConstant.SP_AGREE_TERMS, true)
            } else {
                toast("请同意相关协议及策略")
                return@setOnClickListener
            }
            val number = edit_text_phone_number.text.toString()

//            if (number.contains("@")) {
//                val email = Utils.isEmail(number)
//                if (!email) {
//                    toast("请输入有效的邮箱")
//                    return@setOnClickListener
//                }
//            } else {
                var isValid = false

                try {
                    val phoneNumber = phoneNumberUtil.parse(number, "CN")
                    isValid = phoneNumberUtil.isValidNumberForRegion(phoneNumber, "CN")

                } catch (e: Throwable) {

                }

                if (!isValid) {
                    toast("请输入有效的手机号")
                    return@setOnClickListener
                }
//            }


            val action = PhoneNumberFragmentDirections
                    .actionPhoneNumberFragmentToVerifyCodeFragment(number)

            findNavController().navigate(action)
        }
    }


}
